package com.lanou.bookstore.order.dao;

import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.utils.C3p0_utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    public Integer addOrder(Order order) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "insert into orders values(null,?,?,null,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, order.getOrdertime());
        ps.setDouble(2, order.getTotal());
        ps.setString(3, order.getUid());
        ps.setString(4, order.getAddress());
        ps.executeUpdate();

        PreparedStatement ps1 = connection.prepareStatement("select last_insert_id() oid");
        ResultSet resultSet = ps1.executeQuery();
        if (resultSet.next()) {
            int oid = resultSet.getInt("oid");
            return oid;
        } else {
            return null;
        }
    }

    public void addOrderItem(OrderItem orderItem) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "insert into orderitem values(null,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, orderItem.getCount());
        ps.setDouble(2, orderItem.getSubtotal());
        ps.setInt(3, orderItem.getOid());
        ps.setInt(4, orderItem.getBid());
        ps.executeUpdate();
    }

    public List<String> findOrderTimeByUid(String uid) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "select ordertime from orders where uid = ? group by ordertime";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, Integer.parseInt(uid));
        ResultSet resultSet = ps.executeQuery();
        List<String> stringList = new ArrayList<>();
        while (resultSet.next()) {
            stringList.add(resultSet.getString("ordertime"));
        }
        return stringList;
    }

    public List<Order> findOrderAllByOrderTime(String str) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "select * from orders where ordertime = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, str);
        ResultSet resultSet = ps.executeQuery();
        List<Order> orderList = new ArrayList<>();
        while (resultSet.next()) {
            Order order = new Order();
            order.setOid(resultSet.getInt("oid"));
            order.setOrdertime(resultSet.getString("ordertime"));
            order.setTotal(resultSet.getDouble("total"));
            order.setState(resultSet.getString("state"));
            order.setUid(resultSet.getString("uid"));
            order.setAddress(resultSet.getString("address"));
            orderList.add(order);
        }
        return orderList;
    }

    public OrderItem findOrderItemAllByOid(Integer oid) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "select * from orderitem where oid = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, oid);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            OrderItem orderItem = new OrderItem();
            orderItem.setIid(resultSet.getInt("iid"));
            orderItem.setCount(resultSet.getInt("count"));
            orderItem.setSubtotal(resultSet.getDouble("subtotal"));
            orderItem.setOid(resultSet.getInt("oid"));
            orderItem.setBid(resultSet.getInt("bid"));
            return orderItem;
        } else {
            return null;
        }
    }

    public void updateOrdersStateByOid(Integer oid) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "update orders set state = 3 where oid = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, oid);
        ps.executeUpdate();
    }

    public Integer findOrderStateByOid(Integer oid) throws SQLException {
        Connection connection = C3p0_utils.getConnection();
        String sql = "select state from orders where oid = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, oid);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            Integer state = resultSet.getInt("state");
            return state;
        } else {
            return null;
        }
    }
}
